<?php
class CollectAction extends CommonAction
{
	public function index(){
		$this->listmap = array();
		$Collectcategory = D('Collectcategory');
		if($this->_get('category_id', 'int') > 0){
			$cateids = $Collectcategory-> getAllIDBypid(intval($_GET['category_id']));
			$this->listmap = "collectcategory_id in(".implode(',',$cateids).")";
			
		}
		$pid_list=$Collectcategory->where(array('pid'=>0))->select();
		
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$Collectcategory->getListToArray1($v['id']);
		}
		$this->assign('pid_list',$pid_list);
		$this->_list();
		if(isMobile()){
			if(intval(session('_newsid')) > 0){
				//用户退回到列表，说明他要重新操作，删除他刚刚选择的记录
			    M('News') -> where(array('id' => intval(session('_newsid')), 'site_id' => $this->siteid)) -> delete();
				session('_newsid', NULL);
			}
			$this->display('indexMobile');
		}else{
			$this->display();
		}
	}
	
	
	
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __COLLECTCATEGORY__ c ON n.collectcategory_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$this->list = $this->dao->alias('n')->join('left join __COLLECTCATEGORY__ c ON n.collectcategory_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->pages=$p->show();
	}
	
	public function collect(){
		if(isset($_POST['url'])){
			$url = $_POST['url'];
			import('@.ORG.Collect.Collect');
			$Collect = new Collect();
			$return = $Collect->collectUrl($url);
			$article['title'] = trim($return['title']);
			
			if($article['title'] == '' || $return['content'] == ''){
				$this->error('您贴粘的URL地址可能不是微信的，系统没有采集到内容！');exit;
			}
			
			$article['content'] = $return['content'];
			$article['category_id'] = (int)M('Category') -> where(array('title' => '我的广告', 'site_id' => $this->siteid)) -> getField('id');
			$article['description'] = $return['page_desc'];
			$article['page_title'] = $return['page_title'];
			$article['img_url'] = $return['img_url'];
			$time = time();
			
			$article['site_id'] = $this -> siteid;
			$article['datetime'] = $time;
			$article['update_time'] = $time;
			//dump($article);
			$newsdb = M('News');
			$ret = M('News') -> add($article);

			$this -> dao -> add($article);
			
			if($ret){
				$this -> success('采集成功，正在跳转，准备下一步', U('collect1', array('id' => $ret)));
			}else{
				$this->error('失败了！'.$newsdb -> getError());
			}
		}
		
		$this->display();
	}
	
	public function collecttonews(){
		
		if(M('News') -> where(array('member_id' => $this->memberInfo['id'])) -> count() >= 3 && intval(session('anonymous')) > 0){
			$this->error('体验用户只能发布3个广告，请购买正式版。');
			exit;
		}
		
			$return = $this->dao -> where(array('id' => $this->_get('id'))) -> find();
			$article['title'] = $return['title'];
			$article['content'] = $return['content'];
			$article['category_id'] = (int)M('Category') -> where(array('title' => '我的广告', 'site_id' => $this->siteid)) -> getField('id');
			//$article['category_id'] = isset($_POST['category_id']) ? intval($_POST['category_id']):0;
			$article['description'] = $return['description'];
			//$article['page_title'] = $return['page_title'];
			$article['img_url'] = $return['img_url'];
			$article['page_title'] = $return['page_title'];
			$time = time();
			$article['site_id'] = $this -> siteid;
			$article['datetime'] = $time;
			$article['update_time'] = $time;
			//dump($article);
			$newsdb = M('News');
			
			
			
			$ret = $newsdb -> add($article);
			session('_newsid', $ret);
			redirect(U('collect1', array('id' => $ret)));
	}
	
	

	
	
	public function collect1(){
		$this->dao = D('News');
		$this->cate_list=D('Category')->getListToArray1(0,$this->siteid);
		$this->pagelist = M('Pages') -> where(array('site_id' => $this->siteid, 'title' => '广告位')) -> select();
		if ($this->_get('id')) {
            $map['id'] = array(
                'eq',
                $this->_get('id')
            );
            $info = $this->dao->where($map)->find();
			
			$this->assign('info', $info);
            $this->display();
        } else {
            $this->error('参数错误');
        }
	}
	public function collect2(){
		$this->dao = D('News');
		$this->cate_list=D('Category')->getListToArray1(0,$this->siteid);
		$this->pagelist = M('Pages') -> where(array('site_id' => $this->siteid, 'title' => '广告位')) -> select();
		if ($this->_get('id')) {
            $map['id'] = array(
                'eq',
                $this->_get('id')
            );
            $info = $this->dao->where($map)->find();
			if($this->info['modules_id'] == 0){
				//查找一个模块
				$moduleslist = M('Modules') -> where(array('pages_id' => $this->pagelist[0]['id'])) -> order('sort') -> find();
				$info['modules_id'] = $moduleslist['id'];
				
			}
			$this->assign('info', $info);
            $this->display($temp);
        } else {
            $this->error('参数错误');
        }
	}
	public function edit(){
		$this->cate_list=D('Collectcategory')->getListToArray1(0);
		
		if ($this->_get('id')) {
            $map['id'] = array(
                'eq',
                $this->_get('id')
            );
            $info = $this->dao->where($map)->find();
			$this->assign('info', $info);
            $this->display($temp);
        } else {
            $this->error('参数错误');
        }
	}
	
	
	//预览内容
	public function read(){
		$this->info = $this->dao -> where(array('id' => $this->_get('id'))) -> find();
		$this->display();
	}
	
	
	
	public function release(){
		$this->dao = D('News');
		if ($this->_post('id')) {
            unset($_POST['site_id']);
			
            $map['id'] = array(
                'eq',
                $this->_post('id')
            );
			if(isset($_POST['modules_id']) && intval($_POST['modules_id']) > 0){
				$_POST['modules_id'] = D('Modules') -> copyself($this->siteid,$this->siteid,array('id' => $_POST['modules_id']),0,0);
			}
			$_POST['member_id'] = $this->memberInfo['id'];
            if (!empty($data)) {
                if (!$this->dao->create($data)) {
                    $this->error($this->dao->getError());
                }
            } else {
                if (!$this->dao->create()) {
                    $this->error($this->dao->getError());
                }
            }
            //dump($data);exit();
            //dump(I('post.'));exit();
			
			
			
			
            $result = $this->dao->save();
            //echo $this->dao->getLastSql();exit();
            if (false !== $result) {
				session('_newsid', NULL);
                $this->success('发布成功,正在跳转预览', getArtLink($this->_post('id'), 0));
            } else {
                $this->error($this->dao->getError());
            }
        } else {
            $this->error('参数错误');
        }
		
	}
	
	//移动到分类
	public function move(){
		if($this->_get('category_id')){
			$category_id=$this->_get('category_id');
			$ids=$this->_get('id');
			$map['id'] = array('in', $ids);
			$data['collectcategory_id']=$category_id;
			$re = $this->dao->where($map)->data($data)->save();
			if($re !== false){
			    $this->success('成功移动',U('index'));
			}else{
				$this->error($this->dao->getError());
			}
		}else{
			$this->error('参数错误，没有选择栏目！');
		}
	}
}